package com.google.code.p.restrpc.client.loadbalance;

import java.util.List;

import com.google.code.p.restrpc.RpcRequest;

/**
 * @author dz
 */
public abstract class AbstractLoadBalanceStrategy implements
                                                 LoadBalanceStrategy {
    
    /**
     * 
     */
    public int select(String serviceName,
                      String methodName,
                      RpcRequest[] requests) {
        if (requests == null || requests.length == 0) {
            return -1;
        }
        if (requests.length == 1) {
            return 0;
        }
        return doSelect(serviceName, methodName, requests);
    }
    
    /**
     * 
     */
    public int select(String serviceName,
                      String methodName,
                      List<RpcRequest> requests) {
        return doSelect(serviceName,
                        methodName,
                        (RpcRequest[]) requests.toArray(new RpcRequest[] {}));
    }
    
    protected abstract int doSelect(String serviceName,
                                    String methodName,
                                    RpcRequest[] requests);
    
}
